
use Data/Estimation_sample.dta, clear
	
	gen nexis_buildings  = ROOFME/PROOFME
		
		
	gen roofratio = ROOFME/ROOFTI
	gen roofratiobuildings = roofratio*nexis_buildings
		
	gen roofrelativeIV = (roofratio*nexis_buildings/[moduleprice])/1000

	
	gen solarlast = cum - new 
	
	rename cum currentcum 
	rename new currentnew 
	
	forval x = 1(1)4 {
	drop contract_quarter 
	
	gen contract_quarter = quarter + `x'
	
	merge m:1 postcode contract_quarter using Data/postcode_quarterinstall.dta	, keepusing(cum new)
	drop if _merge ==2
	drop _merge 
	
	rename cum lead`x'cum 
	rename new lead`x'new 
	
	replace lead`x'cum  = lead`x'cum /1000
	replace lead`x'new  = lead`x'new /1000
	
	}
	
	drop contract_quarter
	
	gen contract_quarter = quarter
	
	rename currentcum cuminstallations
	rename currentnew newinstallations
	
	
	xi: xtivreg2 green_p newinstall solarlast  i.quarter  [aweight =NCustomer	] , fe cluster(postcode) 

	
 
	quietly summ green_plan [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	outreg2 using Analysis/FE_postcode_newcum, bdec(3) label ctitle("FE")   ///
	keep(*cum* solarlast *new*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar')  ///
	tex(fragment pretty) nonotes replace
 

	
	xi: xtivreg2 green_p newinstall solarlast lead*new  i.quarter  [aweight =NCustomer	] , fe cluster(postcode) 

	
	
	quietly summ green_plan [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	outreg2 using Analysis/FE_postcode_newcum, bdec(3) label ctitle("FE")   ///
	keep(*cum* solarlast *new*  ) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append
 
	
	
	
	
	
	
	
	
	
	gen event_time = quarter - tq(2011q1) if quarter< tq(2011q1) 	
	replace event_time = quarter- tq(2013q1) if quarter > tq(2013q1)
	replace event_time = 0 if quarter >= tq(2011q1) & quarter<=  tq(2013q1)
	
	quietly tab event_time, gen(newevents)
	
	forval i = 1(1)19 {
	
	gen eventcum`i' = (newevents`i' ==1)* cuminstall
	
	
	
	}
	
	
	
	
	
	gen laghigh_cum = (event_time<0 )*l.cuminstall
	gen lagchange_cum = (event_time ==0 )* l.cuminstall
	gen laglow_cum =( event_time >0) * l.cuminstall
	
	
	
	
	gen high_new = (event_time<0 )*newinstall
	gen change_new = (event_time ==0 )* newinstall
	gen low_new =( event_time >0) * newinstall
	
	
	
		xi: xtivreg2 green_p lag*cum *_new i.quarter  [aweight =NCustomer	] , fe cluster(postcode) 

		
	quietly summ green_plan  [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	
	outreg2 using Analysis/FE_postcode_newcummotive, bdec(3) label ctitle("FE")   ///
	keep(*cum* *new*) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes replace
 
	
	
	
	
	
	
	
	
		xi: xtivreg2 green_p lag*cum *_new i.quarter lead*new  [aweight =NCustomer	] , fe cluster(postcode) 

		
	
	quietly summ green_plan  [aweight =NCust	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_newcummotive, bdec(3) label ctitle("FE")   ///
	keep(*cum* *new* ) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append
 
	
	
	
	
	
	
	
	
	
	
		
